package tw.asap.plus.ui.custom;

import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;

/**
 * Created by yamlee on 15/8/13.
 * 正在上传的图片控件,选择图片进行上传是,上面有上传进度,暂定上传,继续上传显示控件
 */
public class UploadImageView extends FrameLayout {
    @InjectView(R.id.dv_upload_img)
    SimpleDraweeView dvUploadImg;
    @InjectView(R.id.progressbar_upload_progress)
    ProgressBar progressbarUploadProgress;
    @InjectView(R.id.iv_delete_upload)
    ImageView ivDeleteUpload;
    @InjectView(R.id.rl_upload_add)
    RelativeLayout rlUploadAdd;
    @InjectView(R.id.iv_error_upload)
    ImageView ivErrorUpload;
    @InjectView(R.id.rr_half_transparent)
    RelativeLayout rlHalfTransparent;

    private UploadImageViewAddListener mUploadImageListener;
    private UploadImageViewDeleteListener mUploadImageViewDeleteListener;

    public void setBindInfoId(int bindInfoId) {
        this.bindInfoId = bindInfoId;
    }

    public int getBindInfoId() {
        return bindInfoId;
    }


    private int bindInfoId;

    public void setOnAddclickListener(UploadImageViewAddListener mOnAddclickListener) {
        this.mUploadImageListener = mOnAddclickListener;
    }

    public void setDeleteClickListener(UploadImageViewDeleteListener mUploadImageViewDeleteListener) {
        this.mUploadImageViewDeleteListener = mUploadImageViewDeleteListener;
    }

    public UploadImageView(Context context) {
        super(context);
        initView(context);
    }

    public UploadImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public UploadImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public ImageView getErrorUploadView() {
        return ivErrorUpload;
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_image_upload, this);
        ButterKnife.inject(view);
        rlUploadAdd.setOnClickListener(v -> {
            mUploadImageListener.onClickAdd(v);
        });
        ivDeleteUpload.setOnClickListener(v -> mUploadImageViewDeleteListener.onClickDelete(v));
    }

    public void showAddLayout(boolean showlayout) {
        if (showlayout) {
            rlUploadAdd.setVisibility(View.VISIBLE);
        } else {
            rlUploadAdd.setVisibility(View.GONE);
        }
    }

    public void showPicLayout(boolean showlayout) {
        if (showlayout) {
            dvUploadImg.setVisibility(View.VISIBLE);
            progressbarUploadProgress.setVisibility(View.VISIBLE);
            rlHalfTransparent.setVisibility(View.VISIBLE);
            ivDeleteUpload.setVisibility(View.VISIBLE);
        } else {
            dvUploadImg.setVisibility(View.GONE);
            progressbarUploadProgress.setVisibility(View.GONE);
            ivDeleteUpload.setVisibility(View.GONE);
            rlHalfTransparent.setVisibility(View.GONE);
        }
    }

    public void setImage(String url) {
        showAddLayout(false);
        showPicLayout(true);
        dvUploadImg.setImageURI(Uri.parse(url));
    }

    /**
     * progress 应该是大于0等于0,小于等于100的整数值
     *
     * @param prgress
     */
    public void setProgress(int prgress) {
        if (prgress > 100) {
            progressbarUploadProgress.setProgress(100);
            return;
        }
        if (prgress < 0) {
            progressbarUploadProgress.setProgress(0);
            return;
        }
        progressbarUploadProgress.setProgress(prgress);
    }

    public void setShowProgress(boolean showProgress) {
        if (showProgress) {
            progressbarUploadProgress.setVisibility(View.VISIBLE);
            rlHalfTransparent.setVisibility(View.VISIBLE);
        } else {
            progressbarUploadProgress.setVisibility(View.GONE);
            rlHalfTransparent.setVisibility(View.GONE);
        }
    }

    public ProgressBar getProgressBar() {
        return progressbarUploadProgress;
    }

    public interface UploadImageViewAddListener {
        void onClickAdd(View v);
    }

    public interface UploadImageViewDeleteListener {
        void onClickDelete(View v);
    }


}
